package com.ruoyi.training.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.training.domain.TrainingImagePortPath;
import com.ruoyi.training.mapper.TrainingImagesMapper;
import com.ruoyi.training.domain.TrainingImages;
import com.ruoyi.training.service.ITrainingImagesService;

/**
 * 镜像管理Service业务层处理
 * 
 * @author rocc
 * @date 2025-06-12
 */
@Service
public class TrainingImagesServiceImpl implements ITrainingImagesService 
{
    @Autowired
    private TrainingImagesMapper trainingImagesMapper;

    /**
     * 查询镜像管理
     * 
     * @param id 镜像管理主键
     * @return 镜像管理
     */
    @Override
    public TrainingImages selectTrainingImagesById(Long id)
    {
        return trainingImagesMapper.selectTrainingImagesById(id);
    }

    /**
     * 查询镜像管理列表
     * 
     * @param trainingImages 镜像管理
     * @return 镜像管理
     */
    @Override
    public List<TrainingImages> selectTrainingImagesList(TrainingImages trainingImages)
    {
        return trainingImagesMapper.selectTrainingImagesList(trainingImages);
    }

    /**
     * 新增镜像管理
     * 
     * @param trainingImages 镜像管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTrainingImages(TrainingImages trainingImages)
    {
        trainingImages.setCreateTime(DateUtils.getNowDate());
        int rows = trainingImagesMapper.insertTrainingImages(trainingImages);
        insertTrainingImagePortPath(trainingImages);
        return rows;
    }

    /**
     * 修改镜像管理
     * 
     * @param trainingImages 镜像管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTrainingImages(TrainingImages trainingImages)
    {
        trainingImages.setUpdateTime(DateUtils.getNowDate());
        trainingImagesMapper.deleteTrainingImagePortPathByImageId(trainingImages.getId());
        insertTrainingImagePortPath(trainingImages);
        return trainingImagesMapper.updateTrainingImages(trainingImages);
    }

    /**
     * 批量删除镜像管理
     * 
     * @param ids 需要删除的镜像管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTrainingImagesByIds(Long[] ids)
    {
        trainingImagesMapper.deleteTrainingImagePortPathByImageIds(ids);
        return trainingImagesMapper.deleteTrainingImagesByIds(ids);
    }

    /**
     * 删除镜像管理信息
     * 
     * @param id 镜像管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTrainingImagesById(Long id)
    {
        trainingImagesMapper.deleteTrainingImagePortPathByImageId(id);
        return trainingImagesMapper.deleteTrainingImagesById(id);
    }

    /**
     * 新增镜像端口路径信息
     * 
     * @param trainingImages 镜像管理对象
     */
    public void insertTrainingImagePortPath(TrainingImages trainingImages)
    {
        List<TrainingImagePortPath> trainingImagePortPathList = trainingImages.getTrainingImagePortPathList();
        Long id = trainingImages.getId();
        if (StringUtils.isNotNull(trainingImagePortPathList))
        {
            List<TrainingImagePortPath> list = new ArrayList<TrainingImagePortPath>();
            for (TrainingImagePortPath trainingImagePortPath : trainingImagePortPathList)
            {
                trainingImagePortPath.setImageId(id);
                list.add(trainingImagePortPath);
            }
            if (list.size() > 0)
            {
                trainingImagesMapper.batchTrainingImagePortPath(list);
            }
        }
    }
}
